//
//  NgSliderView.m
//  NgTopOSShove
//
//  Created by yscompany on 2018/11/21.
//  Copyright © 2018 NgTopOS. All rights reserved.
//

#import "NgSliderView.h"
#import "NgSliderCollectionViewCell.h"

@interface NgSliderView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *baseCollectionView;
@property (nonatomic, strong) UIView *itemIndirectryView; //指示view
@end
static NSString * reuseIdentifierSliderCellID = @"NgSliderCollectionViewCell";
@implementation NgSliderView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setSubViews];
    }
    return self;
}
- (void)setSubViews {
    [self addSubview:self.baseCollectionView];
    self.itemIndirectryView.frame = CGRectMake(20, CGRectGetHeight(self.frame) - 15, 32, 2);
    [self.baseCollectionView addSubview:self.itemIndirectryView];
}

#pragma mark - collectionviewDelegate datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.itemsArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NgSliderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifierSliderCellID forIndexPath:indexPath];
    if (self.itemsArray.count > indexPath.row) {
        [cell configureTitle:self.itemsArray[indexPath.row]];
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(70, CGRectGetHeight(self.frame));
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [UIView animateWithDuration:.1 animations:^{
        self.itemIndirectryView.frame = CGRectMake(indexPath.row * 80 + 20, CGRectGetHeight(self.frame) - 15, 32, 2);
    }];
    if (self.delegate && [self.delegate respondsToSelector:@selector(setSelectIndex:)]) {
        [self.delegate setSelectIndex:indexPath.row]; //返回当前选择是哪一种产品
    }
}
#pragma mark - outer method
- (void)getSliderPageIndex:(NSInteger)index {
//  获取当前滑动手势停止后所属item类目
    if (index < self.itemsArray.count) {
        [self.baseCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:MAX(0, index) inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        [UIView animateWithDuration:.1 animations:^{
            self.itemIndirectryView.frame = CGRectMake(index * 80 + 20, CGRectGetHeight(self.frame) - 15, 32, 2);
        }];
    }
}
#pragma mark - getter method
- (UICollectionView *)baseCollectionView {
    if (!_baseCollectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _baseCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame) > self.itemsArray.count * 80 ?CGRectGetWidth(self.frame):self.itemsArray.count * 80, CGRectGetHeight(self.frame)) collectionViewLayout:layout];
        [_baseCollectionView registerClass:[NgSliderCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifierSliderCellID];
        layout.itemSize = CGSizeMake(80, CGRectGetHeight(self.frame));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _baseCollectionView.backgroundColor = [UIColor whiteColor];
        _baseCollectionView.showsHorizontalScrollIndicator = NO;
//        _baseCollectionView.
        _baseCollectionView.delegate = self;
        _baseCollectionView.dataSource = self;
    }
    return _baseCollectionView;
}
- (UIView *)itemIndirectryView {
    if (!_itemIndirectryView) {
        _itemIndirectryView = [UIView new];
        _itemIndirectryView.backgroundColor = [UIColor blackColor];
    }
    return _itemIndirectryView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
